package org.rocky.consumer.service;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author Rocky
 * @date 2020/2/17 17:18
 */

@Component
@PropertySource("classpath:queues.properties")
public class RejectDirect {
    @RabbitHandler
    @RabbitListener(queues = "${today.rocky.direct-queue}", containerFactory="rabbitListenerContainerFactory")
    public void processDirect(Message message, Channel channel) throws IOException {
        System.out.println("Direct Queue received msg : " + new String(message.getBody()));
        //true 重回队列--false 销毁该消息
        channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
        channel.basicPublish("DEADLINE_EXCHANGE","#",new AMQP.BasicProperties(),message.getBody());
    }
}
